package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tongatong
 * @name CustomerController
 * @description
 * @date 2021/10/15
 * @time 10:21
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Resource
    private CustomerService customerService;

    @PostMapping("/list")
    public Map<String,Object> list(Integer page, Integer rows, String customerName) {
        System.out.println("-------------------");
        System.out.println(customerName);
        System.out.println("-------------------");
        Map<String, Object> result = new HashMap<>();
        PageInfo<Customer> pageInfo = customerService.list(page, rows, customerName);
        result.put("total", pageInfo.getTotal());
        result.put("rows", pageInfo.getList());
        return result;
    }
    @PostMapping("/save")
    public ServiceVO<Void> save(Customer customer, Integer customerId) {
        System.out.println("---------------------");
        System.out.println(customer);
        System.out.println(customerId);
        System.out.println("---------------------");

        customerService.save(customer, customerId);


        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
    @PostMapping("/delete")
    public ServiceVO<Void> delete(String ids) {
        customerService.delete(ids);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
